﻿//Copyright (c) 2009, Offbeat Guides
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are 
//permitted provided that the following conditions are met:
//
//* Redistributions of source code must retain the above copyright notice, this list of 
//conditions and the following disclaimer.
//
//* Redistributions in binary form must reproduce the above copyright notice, this list 
//of conditions and the following disclaimer in the documentation and/or other materials 
//provided with the distribution.
//
//* Neither the name of Offbeat Guides nor the names of its contributors may be used to 
//endorse or promote products derived from this software without specific prior written 
//permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
//SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
//OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
//HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
//TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Amazon.Services.S3;
using Amazon.Services;
using Amazon.WebServices.UnitTest.Properties;

namespace Amazon.WebServices.UnitTest
{
    [TestClass]
    public class S3File
    {
        #region Properties
        public AmazonAccount Account { get; private set; }
        #endregion

        [TestInitialize]
        public void Initialize()
        {
            this.Account = new AmazonAccount(Settings.Default.AccessID, Settings.Default.SecretAccessID);

            if (!DriveInfo.Exists(this.Account, "offbeat.Test_debug"))
                DriveInfo.Create(this.Account, "offbeat.Test_debug");
        }

        [TestMethod]
        public void CreateFile_1()
        {
            if (File.Exists(this.Account, "offbeat.Test_debug:\\createfile.1.txt"))
                File.Delete(this.Account, "offbeat.Test_debug:\\createfile.1.txt");

            Byte[] buffer = System.Text.Encoding.Default.GetBytes("Hello World!");

            FileInfo file = new FileInfo(this.Account, "offbeat.Test_debug:\\createfile.1.txt");
            Assert.IsFalse(file.Exists);
            using (FileStream stream = file.Create())
            {
                stream.Write(buffer, 0, buffer.Length);
                stream.Flush();
            }
            Assert.IsTrue(file.Exists);
            Assert.IsTrue(File.Exists(this.Account, "offbeat.Test_debug:\\createfile.1.txt"));

            using (System.IO.StreamReader reader = new System.IO.StreamReader(file.OpenRead()))
                Assert.AreEqual("Hello World!", reader.ReadToEnd());

            file.Delete();
            Assert.IsFalse(file.Exists);
            Assert.IsFalse(File.Exists(this.Account, "offbeat.Test_debug:\\createfile.1.txt"));
        }

        [TestMethod]
        public void CreateFile_2()
        {
            if (File.Exists(this.Account, "offbeat.Test_debug:\\createfile.2.txt"))
                File.Delete(this.Account, "offbeat.Test_debug:\\createfile.2.txt");

            Byte[] buffer = System.Text.Encoding.Default.GetBytes("Hello World!");

            File.WriteAllBytes(this.Account, "offbeat.Test_debug:\\createfile.2.txt", buffer);
            Assert.IsTrue(File.Exists(this.Account, "offbeat.Test_debug:\\createfile.2.txt"));

            Assert.AreEqual("Hello World!", File.ReadAllText(this.Account, "offbeat.Test_debug:\\createfile.2.txt"));

            File.Delete(this.Account, "offbeat.Test_debug:\\createfile.2.txt");
            Assert.IsFalse(File.Exists(this.Account, "offbeat.Test_debug:\\createfile.2.txt"));
        }

        [TestMethod]
        public void CreateFile_3()
        {
            if (File.Exists(this.Account, "offbeat.Test_debug:\\createfile.3.txt"))
                File.Delete(this.Account, "offbeat.Test_debug:\\createfile.3.txt");

            Byte[] buffer = System.Text.Encoding.Default.GetBytes("Hello World!");

            FileStream stream = File.Create(this.Account, "offbeat.Test_debug:\\createfile.3.txt", buffer.Length);
            Assert.IsTrue(File.Exists(this.Account, "offbeat.Test_debug:\\createfile.3.txt"));

            stream.Write(buffer, 0, buffer.Length);
            stream.Flush();
            Assert.IsTrue(File.Exists(this.Account, "offbeat.Test_debug:\\createfile.3.txt"));

            using (System.IO.StreamReader reader = new System.IO.StreamReader(File.OpenRead(this.Account, "offbeat.Test_debug:\\createfile.3.txt")))
                Assert.AreEqual("Hello World!", reader.ReadToEnd());

            File.Delete(this.Account, "offbeat.Test_debug:\\createfile.3.txt");
            Assert.IsFalse(File.Exists(this.Account, "offbeat.Test_debug:\\createfile.3.txt"));
        }

        [TestMethod]
        public void FileStreamTest()
        {
            AmazonAccount account = new AmazonAccount(Settings.Default.AccessID, Settings.Default.SecretAccessID);

            if (!DriveInfo.Exists(account, "Test_debug"))
                DriveInfo.Create(account, "Test_debug");
            if (Directory.Exists(account, "Test_debug:\\sub1\\"))
                Directory.Delete(account, "Test_debug:\\sub1\\", true);

            Byte[] buffer = System.Text.Encoding.UTF8.GetBytes("Hello World!");

            FileStream stream = new FileStream(account, "Test_debug:\\sub1\\test.txt", System.IO.FileMode.Create);
            stream.Write(buffer, 0, buffer.Length);
            stream.Flush();
            stream.Close();

            String text = File.ReadAllText(account, "Test_debug:\\sub1\\test.txt");
            Assert.AreEqual<String>(text, "Hello World!");
        }
    }
}
